home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / mysql-4.0.22-win / data1.cab / Development / examples / tests / test_delayed_insert.pl < prev    next >
Encoding:
Perl Script  |  2004-10-28  |  10.1 KB  |  366 lines

  1. #!/usr/bin/perl -w
  2.  
  3. # This is a test for INSERT DELAYED
  4. #
  5.  
  6. $opt_loop_count=10000; # Change this to make test harder/easier
  7.  
  8. ##################### Standard benchmark inits ##############################
  9.  
  10. use DBI;
  11. use Getopt::Long;
  12. use Benchmark;
  13.  
  14. package main;
  15.  
  16. $opt_skip_create=$opt_skip_in=$opt_verbose=$opt_fast_insert=
  17.   $opt_lock_tables=$opt_debug=$opt_skip_delete=$opt_fast=$opt_force=0;
  18. $opt_host=$opt_user=$opt_password=""; $opt_db="test";
  19.  
  20. GetOptions("host=s","db=s","loop-count=i","skip-create","skip-in","skip-delete",
  21. "verbose","fast-insert","lock-tables","debug","fast","force") || die "Aborted";
  22. $opt_verbose=$opt_debug=$opt_lock_tables=$opt_fast_insert=$opt_fast=$opt_skip_in=$opt_force=undef;  # Ignore warnings from these
  23.  
  24. print "Testing 8 multiple connections to a server with 1 insert, 2 delayed\n";
  25. print "insert, 1 update, 1 delete, 1 flush tables and 3 select connections.\n";
  26.  
  27. $firsttable  = "bench_f1";
  28. $secondtable = "bench_f2";
  29.  
  30. ####  
  31. ####  Start timeing and start test
  32. ####
  33.  
  34. $start_time=new Benchmark;
  35. if (!$opt_skip_create)
  36. {
  37.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  38.   $Mysql::QUIET = 1;
  39.   $dbh->do("drop table if exists $firsttable,$secondtable");
  40.   $Mysql::QUIET = 0;
  41.  
  42.   print "Creating tables $firsttable and $secondtable in database $opt_db\n";
  43.   $dbh->do("create table $firsttable (id int(6) not null, info varchar(32), marker char(1), primary key(id))") or die $DBI::errstr;
  44.   $dbh->do("create table $secondtable (id int(6) not null, row int(3) not null,value double, primary key(id,row))") or die $DBI::errstr;
  45.   
  46.   $dbh->disconnect;
  47. }
  48. $|= 1;                # Autoflush
  49.  
  50. ####
  51. #### Start the tests
  52. ####
  53.  
  54. test_1() if (($pid=fork()) == 0); $work{$pid}="insert";
  55. test_delayed_1() if (($pid=fork()) == 0); $work{$pid}="delayed_insert1";
  56. test_delayed_2() if (($pid=fork()) == 0); $work{$pid}="delayed_insert2";
  57. test_2() if (($pid=fork()) == 0); $work{$pid}="update";
  58. test_3() if (($pid=fork()) == 0); $work{$pid}="select1";
  59. test_4() if (($pid=fork()) == 0); $work{$pid}="select2";
  60. test_5() if (($pid=fork()) == 0); $work{$pid}="select3";
  61. test_del() if (($pid=fork()) == 0); $work{$pid}="delete";
  62. test_flush() if (($pid=fork()) == 0); $work{$pid}="flush";
  63.  
  64. $errors=0;
  65. while (($pid=wait()) != -1)
  66. {
  67.   $ret=$?/256;
  68.   print "thread '" . $work{$pid} . "' finished with exit code $ret\n";
  69.   $errors++ if ($ret != 0);
  70. }
  71.  
  72. if (!$opt_skip_delete && !$errors)
  73. {
  74.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  75.   $dbh->do("drop table $firsttable");
  76.   $dbh->do("drop table $secondtable");
  77. }
  78. print ($errors ? "Test failed\n" :"Test ok\n");
  79.  
  80. $end_time=new Benchmark;
  81. print "Total time: " .
  82.   timestr(timediff($end_time, $start_time),"noc") . "\n";
  83.  
  84. exit(0);
  85.  
  86. #
  87. # Insert records in the two tables
  88.  
  89. sub test_1
  90. {
  91.   my ($dbh,$tmpvar,$rows,$found,$i);
  92.  
  93.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  94.   $tmpvar=1;
  95.   $rows=$found=0;
  96.   for ($i=0 ; $i < $opt_loop_count; $i++)
  97.   {
  98.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  99.     $dbh->do("insert into $firsttable values ($i,'This is entry $i','')") || die "Got error on insert: $DBI::errstr\n";
  100.     $row_count=($i % 7)+1;
  101.     $rows+=1+$row_count;
  102.     for ($j=0 ; $j < $row_count; $j++)
  103.     {
  104.       $dbh->do("insert into $secondtable values ($i,$j,0)") || die "Got error on insert: $DBI::errstr\n";
  105.     }
  106.   }
  107.   $dbh->disconnect;
  108.   print "Test_1: Inserted $rows rows\n";
  109.   exit(0);
  110. }
  111.  
  112.  
  113. sub test_delayed_1
  114. {
  115.   my ($dbh,$tmpvar,$rows,$found,$i,$id);
  116.  
  117.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  118.   $tmpvar=1;
  119.   $rows=$found=0;
  120.   for ($i=0 ; $i < $opt_loop_count; $i++)
  121.   {
  122.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  123.     $id=$i+$opt_loop_count;
  124.     $dbh->do("insert delayed into $firsttable values ($id,'This is entry $id','')") || die "Got error on insert: $DBI::errstr\n";
  125.     $row_count=($i % 7)+1;
  126.     $rows+=1+$row_count;
  127.     for ($j=0 ; $j < $row_count; $j++)
  128.     {
  129.       $dbh->do("insert into $secondtable values ($id,$j,0)") || die "Got error on insert: $DBI::errstr\n";
  130.     }
  131.     if (($tmpvar % 100) == 0)
  132.     {
  133.       $dbh->do("select max(info) from $firsttable") || die "Got error on select max(info): $DBI::errstr\n";
  134.       $dbh->do("select max(value) from $secondtable") || die "Got error on select max(info): $DBI::errstr\n";      
  135.       $found+=2;
  136.     }
  137.   }
  138.   $dbh->disconnect;
  139.   print "Test_1: Inserted delayed $rows rows, found $found rows\n";
  140.   exit(0);
  141. }
  142.  
  143.  
  144. sub test_delayed_2
  145. {
  146.   my ($dbh,$tmpvar,$rows,$found,$i,$id);
  147.  
  148.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  149.   $tmpvar=1;
  150.   $rows=$found=0;
  151.   for ($i=0 ; $i < $opt_loop_count; $i++)
  152.   {
  153.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  154.     $id=$i+$opt_loop_count*2;
  155.     $dbh->do("insert delayed into $firsttable values ($id,'This is entry $id','')") || die "Got error on insert: $DBI::errstr\n";
  156.     $row_count=($i % 7)+1;
  157.     $rows+=1+$row_count;
  158.     for ($j=0 ; $j < $row_count; $j++)
  159.     {
  160.       $dbh->do("insert delayed into $secondtable values ($id,$j,0)") || die "Got error on insert: $DBI::errstr\n";
  161.     }
  162.     if (($tmpvar % 100) == 0)
  163.     {
  164.       $dbh->do("select max(info) from $firsttable") || die "Got error on select max(info): $DBI::errstr\n";
  165.       $dbh->do("select max(value) from $secondtable") || die "Got error on select max(info): $DBI::errstr\n";      
  166.       $found+=2;
  167.     }
  168.   }
  169.   $dbh->disconnect;
  170.   print "Test_1: Inserted delayed $rows rows, found $found rows\n";
  171.   exit(0);
  172. }
  173.  
  174. #
  175. # Update records in both tables
  176. #
  177.  
  178. sub test_2
  179. {
  180.   my ($dbh,$id,$tmpvar,$rows,$found,$i,$max_id,$tmp,$sth,$count);
  181.  
  182.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  183.   $tmpvar=111111;
  184.   $rows=$found=$max_id=$id=0;
  185.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  186.   {
  187.     $tmp=(($tmpvar + 63) + $i)*3;
  188.     $tmp=$tmp-int($tmp/100000)*100000; 
  189.     $tmpvar^= $tmp;
  190.     $tmp=$tmpvar - int($tmpvar/10)*10;
  191.     if ($max_id*$tmp == 0)
  192.     {
  193.       $max_id=0;
  194.       $sth=$dbh->prepare("select max(id) from $firsttable where marker=''");
  195.       $sth->execute() || die "Got error select max: $DBI::errstr\n";
  196.       if ((@row = $sth->fetchrow_array()) && defined($row[0]))
  197.       {
  198.     $found++;
  199.     $max_id=$id=$row[0];
  200.       }
  201.       $sth->finish;
  202.     }
  203.     else
  204.     {
  205.       $id= $tmpvar % ($max_id-1)+1;
  206.     }
  207.     if ($id)
  208.     {
  209.       ($count=$dbh->do("update $firsttable set marker='x' where id=$id")) || die "Got error update $firsttable: $DBI::errstr\n";
  210.       $rows+=$count;
  211.       if ($count > 0)
  212.       {
  213.     $count=$dbh->do("update $secondtable set value=$i where id=$id") || die "Got error update $firsttable: $DBI::errstr\n";
  214.     $rows+=$count;
  215.       }
  216.     }
  217.   }
  218.   $dbh->disconnect;
  219.   print "Test_2: Found $found rows, Updated $rows rows\n";
  220.   exit(0);
  221. }
  222.  
  223.  
  224. #
  225. # select records
  226. #
  227.  
  228. sub test_3
  229. {
  230.   my ($dbh,$id,$tmpvar,$rows,$i,$count);
  231.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  232.   $tmpvar=222222;
  233.   $rows=0;
  234.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  235.   {
  236.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  237.     $id=$tmpvar % $opt_loop_count;
  238.     $count=$dbh->do("select id from $firsttable where id=$id") || die "Got error on select from $firsttable: $DBI::errstr\n";
  239.     $rows+=$count;
  240.   }
  241.   $dbh->disconnect;
  242.   print "Test_3: Found $rows rows\n";
  243.   exit(0);
  244. }
  245.  
  246.  
  247. #
  248. # Note that this uses row=1 and in some cases won't find any matching
  249. # records
  250. #
  251.  
  252. sub test_4
  253. {
  254.   my ($dbh,$id,$tmpvar,$rows,$i,$count);
  255.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  256.   $tmpvar=333333;
  257.   $rows=0;
  258.   for ($i=0 ; $i < $opt_loop_count; $i++)
  259.   {
  260.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  261.     $id=$tmpvar % $opt_loop_count;
  262.     $count=$dbh->do("select id from $secondtable where id=$id") || die "Got error on select from $secondtable: $DBI::errstr\n";
  263.     $rows+=$count;
  264.   }
  265.   $dbh->disconnect;
  266.   print "Test_4: Found $rows rows\n";
  267.   exit(0);
  268. }
  269.  
  270.  
  271. sub test_5
  272. {
  273.   my ($dbh,$id,$tmpvar,$rows,$i,$max_id,$count,$sth);
  274.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  275.   $tmpvar=444444;
  276.   $rows=$max_id=0;
  277.   for ($i=0 ; $i < $opt_loop_count ; $i++)
  278.   {
  279.     $tmpvar^= ((($tmpvar + 63) + $i)*3 % 100000);
  280.     if ($max_id == 0 || ($tmpvar % 10 == 0))
  281.     {
  282.       $sth=$dbh->prepare("select max(id) from $firsttable");
  283.       $sth->execute() || die "Got error select max: $DBI::errstr\n";
  284.       if ((@row = $sth->fetchrow_array()) && defined($row[0]))
  285.       {
  286.     $max_id=$id=$row[0];
  287.       }
  288.       else
  289.       {
  290.     $id=0;
  291.       }
  292.       $sth->finish;
  293.     }
  294.     else
  295.     {
  296.       $id= $tmpvar % $max_id;
  297.     }
  298.     $count=$dbh->do("select value from $firsttable,$secondtable where $firsttable.id=$id and $secondtable.id=$firsttable.id") || die "Got error on select from $secondtable: $DBI::errstr\n";
  299.     $rows+=$count;
  300.   }
  301.   $dbh->disconnect;
  302.   print "Test_5: Found $rows rows\n";
  303.   exit(0);
  304. }
  305.  
  306.  
  307. #
  308. # Delete the smallest row
  309. #
  310.  
  311. sub test_del
  312. {
  313.   my ($dbh,$min_id,$i,$sth,$rows);
  314.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host") || die $DBI::errstr;
  315.   $rows=0;
  316.   for ($i=0 ; $i < $opt_loop_count/3; $i++)
  317.   {
  318.     $sth=$dbh->prepare("select min(id) from $firsttable");
  319.     $sth->execute() || die "Got error on select from $firsttable: $DBI::errstr\n";
  320.     if ((@row = $sth->fetchrow_array()) && defined($row[0]))
  321.     {
  322.       $min_id=$row[0];
  323.     }
  324.     $sth->finish;
  325.     $dbh->do("delete from $firsttable where id = $min_id") || die "Got error on DELETE from $firsttable: $DBI::errstr\n";
  326.     $rows++;
  327.   }
  328.   $dbh->disconnect;
  329.   print "Test_del: Deleted $rows rows\n";
  330.   exit(0);
  331. }
  332.  
  333.  
  334. #
  335. # Do a flush tables once in a while
  336. #
  337.  
  338. sub test_flush
  339. {
  340.   my ($dbh,$sth,$found1,$last_found1,$i,@row);
  341.   $found1=0; $last_found1=-1;
  342.  
  343.   $dbh = DBI->connect("DBI:mysql:$opt_db:$opt_host",
  344.               $opt_user, $opt_password,
  345.             { PrintError => 0}) || die $DBI::errstr;
  346.  
  347.   for ($i=0; $found1 != $last_found1 ; $i++)
  348.   {
  349.     $sth=$dbh->prepare("flush tables") || die "Got error on prepare: $dbh->errstr\n";
  350.     $sth->execute || die $dbh->errstr;    
  351.     $sth->finish;
  352.  
  353.     $sth=$dbh->prepare("select count(*) from $firsttable") || die "Got error on prepare: $dbh->errstr\n";
  354.     $sth->execute || die $dbh->errstr;
  355.     @row = $sth->fetchrow_array();
  356.     $last_found1=$found1;
  357.     $found1= $row[0];
  358.     $sth->finish;
  359.     sleep(5);
  360.   }
  361.   $dbh->disconnect; $dbh=0;
  362.   print "flush: Did $i repair/checks\n";
  363.   exit(0);
  364. }
  365.